package com.lastoffer2;

import java.util.ArrayList;
import java.util.List;

import com.example.lastoffer2.R;
import com.lastct.dao.Offer;
import com.lastct.dao.OfferTypes;
import com.lastoffer2.adapter.GridviewAdapter;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.widget.GridView;

public class MainActivity extends Activity {

	private ProgressDialog pd;
	public List<OfferTypes> data;
	private GridviewAdapter gridViewAdapter;
    private ArrayList<String> listTypes;
    private ArrayList<String> listImages;
 
    private GridView gridView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		/*
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	   */
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);
		
		// Start a new thread that will download all the data
		new DownloadTask().execute("Any parameters my download task needs here");
 
        
 
        
 
        // Implement On Item click listener
       /* gridView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                Toast.makeText(GridViewExampleActivity.this, gridViewAdapter.getItem(position), Toast.LENGTH_SHORT).show();
            }
        });
        */
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private class DownloadTask extends AsyncTask<String, Void, Object> {
        protected Object doInBackground(String... args) {
            Log.i("MyApp", "Background thread starting");
            //List<Offer> result = JSONConnector.getListOffers();
            // This is where you would do all the work of downloading your data
            List<OfferTypes> result=JSONConnector.getListOfferTypes();
            return result;
        }

        protected void onPostExecute(Object result) {
            // Pass the result data back to the main activity
        	MainActivity.this.data = (List<OfferTypes>)result;
        	
        	prepareGridViewList();
        	
        	// prepared arraylist and passed it to the Adapter class
            gridViewAdapter = new GridviewAdapter(MainActivity.this,listTypes, listImages);
     
            // Set custom adapter to gridview
            gridView = (GridView) findViewById(R.id.gridView1);
            gridView.setAdapter(gridViewAdapter);
            
            if (MainActivity.this.pd != null) {
            	MainActivity.this.pd.dismiss();
            }
        }
   }
   
	public void prepareGridViewList()
    {
		listTypes = new ArrayList<String>();
		listImages = new ArrayList<String>();
		for(OfferTypes ot:data){
			listTypes.add(ot.description);
			listImages.add(ot.image);
			
		}
		/*
          listTypes.add("Brazil");
          listTypes.add("Canada");
          listTypes.add("China");
          listTypes.add("France");
          listTypes.add("Germany");
          listTypes.add("Iran");
          listTypes.add("Italy");
          listTypes.add("Japan");
          listTypes.add("Korea");
          listTypes.add("Mexico");
          listTypes.add("Netherlands");
          listTypes.add("Portugal");
          listTypes.add("Russia");
          listTypes.add("Saudi Arabia");
          listTypes.add("Spain");
          listTypes.add("Turkey");
          listTypes.add("United Kingdom");
          listTypes.add("United States");
 
          listImages.add(R.drawable.india);
          listImages.add(R.drawable.brazil);
          listImages.add(R.drawable.canada);
          listImages.add(R.drawable.china);
          listImages.add(R.drawable.france);
          listImages.add(R.drawable.germany);
          listImages.add(R.drawable.iran);
          listImages.add(R.drawable.italy);
          listImages.add(R.drawable.japan);
          listImages.add(R.drawable.korea);
          listImages.add(R.drawable.mexico);
          listImages.add(R.drawable.netherlands);
          listImages.add(R.drawable.portugal);
          listImages.add(R.drawable.russia);
          listImages.add(R.drawable.saudi_arabia);
          listImages.add(R.drawable.spain);
          listImages.add(R.drawable.turkey);
          listImages.add(R.drawable.united_kingdom);
          listImages.add(R.drawable.united_states);
          */
    }

}
